# Contributor: Frederic Wang <fred.wang@free.fr>

_realname=pytest
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=5.4.1
pkgrel=1
pkgdesc='simple powerful testing with Python (mingw-w64)'
url='https://pytest.org/'
license=('MIT')
arch=('any')
install=${_realname}3-${CARCH}.install
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-atomicwrites>=1.0"
         "${MINGW_PACKAGE_PREFIX}-python-attrs>=17.4.0"
         "${MINGW_PACKAGE_PREFIX}-python-more-itertools>=4.0.0"
         "${MINGW_PACKAGE_PREFIX}-python-pluggy>=0.7"
         "${MINGW_PACKAGE_PREFIX}-python-py>=1.5.0"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-python-colorama"
         "${MINGW_PACKAGE_PREFIX}-python-wcwidth")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools-scm")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-hypothesis"
              "${MINGW_PACKAGE_PREFIX}-python-mock"
              "${MINGW_PACKAGE_PREFIX}-python-nose"
              "${MINGW_PACKAGE_PREFIX}-python-pytest-xdist"
              "${MINGW_PACKAGE_PREFIX}-python-requests"
              "${MINGW_PACKAGE_PREFIX}-python-tox"
              "${MINGW_PACKAGE_PREFIX}-python-yaml")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/pytest-dev/pytest/archive/${pkgver}.tar.gz")
sha256sums=('301a64409d89eb829254ed9f7531cdf921be5d0fcd50ae2dc785f4ce26248e6b')

prepare() {
  sed -i "s/'_pytest.vendored_packages'//" ${_realname}-${pkgver}/setup.py
  sed -i 's/assert numentries == 0/assert numentries == 26/' ${_realname}-${pkgver}/testing/python/collect.py
  export SETUPTOOLS_SCM_PRETEND_VERSION=${pkgver}

  rm -rf python-build-${CARCH}
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

# Note that build() is sometimes skipped because it's done in 
# the packages setup.py install for simplicity if you can do so.
# but sometimes, you want to do a check before install which would
# also trigger the build.
build() {  
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/python3-build-${CARCH}"
  # Python3: 1 failed, 2361 passed, 88 skipped, 12 xfailed, 1 warnings
  MSYSTEM= PYTHONPATH="$(pwd)/build/lib" \
    ${MINGW_PREFIX}/bin/python src/pytest.py || warning "Test failed"
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/COPYING"

  # You should OMIT this section if your package is not installing anything in the bin dir.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _ff in ${pkgdir}${MINGW_PREFIX}/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_ff}
  done
}
